package com.bizfx.component.views
{
	import com.bizfx.collections.HashMap;
	import com.bizfx.events.NavigatorItemEvent;
	import com.bizfx.events.NavigatorSelectionChangedEvent;
	import com.bizfx.utils.StringUtils;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	import mx.containers.VBox;
	import mx.controls.Tree;
	import mx.events.ListEvent;
	import mx.events.TreeEvent;
	
	[Event(name="itemAdding", type="com.bizfx.events.NavigatorItemEvent")]
	[Event(name="itemAdded", type="com.bizfx.events.NavigatorItemEvent")]
	[Event(name="itemClick", type="com.bizfx.events.NavigatorItemEvent")]
	[Event(name="selectionChanged", type="com.bizfx.events.NavigatorSelectionChangedEvent")]
	
	[Component(id="simpleTreeNavigator")]
	public class SimpleTreeNavigator extends BaseNavigator implements INavigator
	{
		private var nodeMap:HashMap = new HashMap();
		
		private var currentNode:NavigatorItemBase = null;
		
		private var container:VBox = new VBox();
		
		private var navigatorTree:Tree = new Tree();
		
		public function get selectedItem():NavigatorItemBase {
			if (this.navigatorTree.selectedItem == null) {
				return null;	
			}
			return NavigatorItemBase(this.navigatorTree.selectedItem);
		}
		
		public function set selectedItem(value:NavigatorItemBase):void {
			this.navigatorTree.selectedItem = NavigatorItemBase(value);
		}
		
		public function SimpleTreeNavigator() {
			super();
			this.viewDataSource = new ArrayCollection
		}
		
		public function selectItemByKey(key:String):void {
		}
		
		public function addItem(item:NavigatorItemBase):void {
			if (item != null) {
				if (item.isLeaf) {
					if (item.hasOwnProperty("children")) {
						delete item["children"];
					}
				} else {
					if (!item.hasOwnProperty("children")) {
						item["children"] = new ArrayCollection();
					} else if (item["children"]) {
						item["children"] = new ArrayCollection();
					}
				}
				if (StringUtils.isNullOrEmpty(item.parentKey)) {
					ArrayCollection(this.viewDataSource).addItem(item);
					this.invalidateProperties();
					this.invalidateDisplayList();
					this.nodeMap.put(item.key, item);
				} else {
					var parentNode:NavigatorItemBase = NavigatorItemBase(this.nodeMap.get(item.parentKey));
					if (parentNode != null) {
						parentNode["children"].addItem(item);
						this.invalidateProperties();
						this.invalidateDisplayList();
						this.nodeMap.put(item.key, item);
						this.navigatorTree.expandItem(parentNode, false, false, false);
						this.navigatorTree.expandItem(parentNode, true, true, false);
					}
				}
			}
		}
		
		public function clearChildren(parentKey:String):void{
			var parentNode:NavigatorItemBase = NavigatorItemBase(this.nodeMap.get(parentKey));
			if (parentNode != null) {
				if (parentNode.hasOwnProperty("children") && parentNode["children"] is IList) {
					IList(parentNode["children"]).removeAll();
				}
			}
		}
		
		public function getItemByKey(key:String):NavigatorItemBase {
			if (this.nodeMap.containsKey(key)) {
				var node:NavigatorItemBase = NavigatorItemBase(this.nodeMap.get(key));
				return node;
			}
			return null;
		}
		
		override public function initialize():void {
			super.initialize();
			container.percentHeight = 100;
			container.percentWidth = 100;
			
			navigatorTree.id = "tv_NavigatorTree";
			navigatorTree.percentHeight = 100;
			navigatorTree.percentWidth = 100;
			navigatorTree.labelField = "text"
			navigatorTree.dataProvider = viewDataSource;
			navigatorTree.showRoot = false;
			navigatorTree.doubleClickEnabled = true;
			navigatorTree.expandItem(navigatorTree.root, true);
			navigatorTree.addEventListener(TreeEvent.ITEM_OPENING, tv_NavigatorTree_ItemOpeningHandler);
			navigatorTree.addEventListener(ListEvent.ITEM_CLICK, tv_NavigatorTree_ClickHandler);
			container.addChild(navigatorTree);
			this.addChild(container);
		}
		
		private function tv_NavigatorTree_ItemOpeningHandler(event:TreeEvent):void {
			if(event.type == TreeEvent.ITEM_OPENING && event.opening) {
				logger.debug("导航器被展开。");
			}
			this.navigatorTree.selectedItem = event.item;
			this.loadNavigatorItem(event.item as NavigatorItemBase);
		}
		
		private function tv_NavigatorTree_ClickHandler(event:ListEvent):void {
			var openingNode:NavigatorItemBase = NavigatorItemBase(this.navigatorTree.selectedItem);
			tv_NavigatorTree_ItemSelected(openingNode);
			var isOpen:Boolean = navigatorTree.isItemOpen(openingNode); 
			navigatorTree.expandItem(openingNode, !isOpen, true);
		}
		
		private function tv_NavigatorTree_ItemSelected(node:NavigatorItemBase):void {
			var clickEvent:NavigatorItemEvent = new NavigatorItemEvent(NavigatorItemEvent.NavigatorItem_Click);
			clickEvent.item = node;
			this.dispatchEvent(clickEvent);
			this.loadNavigatorItem(node);
		}
		
		private function loadNavigatorItem(node:NavigatorItemBase):void {
			if (this.currentNode == null || this.currentNode.key != node.key) {
				var checkChangeEvent:NavigatorSelectionChangedEvent = new NavigatorSelectionChangedEvent();
				checkChangeEvent.oldItem = this.currentNode;
				checkChangeEvent.newItem = node;
				this.dispatchEvent(checkChangeEvent);
				this.currentNode = node;
				this.currentNode.hasLoadedChildren = true;
			}
		}
	}
}